﻿$('html').addClass('rss-js'); $(document).ready(function() {
    var els = $("img.main:first");
    var rhEl = $("#rss-tert");
    
    if ((els.length > 0) && (rhEl.length > 0)) {
        var offset = els.offset();
        rhEl.css({ top: offset.top + "px" });
    } else {
        var qHeight = $("#rss-quote").outerHeight();
        var qOffset = $("#rss-quote").offset();
        var qTotal = qHeight + qOffset.top;
        rhEl.css({ top: (qTotal + 24) + "px" });
    }
    
    var formEl = $("#rss-contact"); 
    
    if (formEl.length > 0) {
        formEl.submit(function() {
            var failErrorMsg = "<p>It appears there were some errors when submitting the contact form.</p>"; failErrorMsg += "<p>Please check that all fields have been filled in, and that you have entered a valid email address.</p>";
            var passed = true;
            var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            $(this).find(":input:not(:hidden)").each(function(i) {
                var errorFlag = false;
                var emailErrorFlag = false;
              
                if ($(this).val() == "") {
                    errorFlag = true; passed = false;
                } else if ($(this).attr("id") == "email") {
                    if (pattern.test($(this).val()) == false) {
                        errorFlag = true; passed = false;
                    }
                }

                if (errorFlag == true) {
                    $(this).parent().addClass('error');
                } else {
                     $(this).parent().removeClass('error'); 
                }
            });
            
            if (passed == false) {
                if ($("#rss-tert").length == 0) {
                    $(this).before('<div id="rss-tert" class="error">' + failErrorMsg + '</div>');
                    var rhEl = $("#rss-tert");
                    var el = $(this).find(":input:not(:hidden):first");
                    var offset = el.offset();
                    rhEl.css({ top: offset.top + "px", opacity: "0" });
                    $(rhEl).animate({ 'opacity': 1 }, 500);
                } }
            return passed;
        });
    }

    $("#rss-nav a, #rss-tert li a").hover(function() {
        $(this).stop().animate({ marginLeft: "8px" }, 250);
        }, function() {
            $(this).stop().animate({ marginLeft: "0" }, 250);
        });
    
        $("ol#rss-content:not('.nonlinking') > li").css("cursor", "pointer").hover(function() {
            $(this).stop().animate({ borderLeftWidth: "12px", paddingLeft: "12px", paddingRight: "0" }, 200).find("a").css({ textDecoration: "underline" });
        }, function() {
            $(this).stop().animate({ borderLeftWidth: "0", paddingLeft: "0", paddingRight: "24px" }, 200).find("a").css({ textDecoration: "none" });
        }).click(function() {
            var myHref = $(this).find("a:first").attr("href");
            window.location.href = myHref;
        });

        var toggleMonth = function($el) {
            var $divEl = $el.parent().next();
            var divHeight = $divEl.outerHeight();
            var targHeight = 0;
            if (divHeight == 0) {
                targHeight = $divEl.children("table").outerHeight(true);
            }
        $divEl.stop().animate({ height: targHeight + "px" }, 500);
    }
    
    var diaryEl = $("ol.year");
    if (diaryEl.length > 0) {
        $("ol.year li h3").wrapInner("<a></a>");
        $("ol.year li h3 a").addClass("twirl").attr("title", "Click to toggle this month's events").click(function() {
            $(this).toggleClass("open");
            toggleMonth($(this));
        });
        $("#rss-content h2").attr("title", "Click to toggle this year's events").css("cursor", "pointer").click(function() {
            var $targEls = $(this).next().find("li h3 a");
            var openEls = $(this).next().find("li h3 a.open").length;
            if ($targEls.length == openEls) {
                $targEls.each(function() {
                    $(this).click();
                });
            } else {
                $targEls.each(function() {
                    if (!$(this).hasClass("open")) {
                        $(this).click();
                    } 
                });
            }
        });
        
        $("ol.year:first li:first h3 a").click(); }
});